﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataBase.Utils;
using System.Windows;
using GreenXi.Model;
using DataBase.DBmodels;
using Prism.Commands;
using Microsoft.EntityFrameworkCore;

namespace GreenXi.ViewModel
{
    public class Craft3ViewModel:CraftModel
    {
        private static int crafttype = 4; //组ID
        private Pages.Setting.Craft_Edit3 CraftEdit;
        public void Init()
        {
            using MyDbContext myDbContext = new();
            GatewayList = new(myDbContext.GetGateway());
            CraftList = new (GlobalVariables.CraftViewList3);
            

        }

        public Craft3ViewModel()
        {
            Init();//初始化

            EditColumn = new DelegateCommand<object>((p) => { EditColumn_Click(p); });//编辑栏目
            SaveColumn = new DelegateCommand<object>((p) => { SaveColumn_Click(p); });//保存栏目
            CloseWindows = new DelegateCommand<object>((p) => { CloseWindows_Click(); });//关闭编辑窗口
            AddProject = new DelegateCommand(() => { AddProject_Click(); });          //新增项目
            DelProject = new DelegateCommand<object>((p) => { DelProject_Click(p); });//删除项目
            EditProject = new DelegateCommand<object>((p) => { EditProject_Click(p); });//编辑项目
            SaveProject = new DelegateCommand<object>((p) => { SaveProject_Click(p); });//保存项目

        }


        #region 项目
        private void CloseWindows_Click()
        {
            CraftEdit.Close();//关闭弹窗
        }
        private void EditColumn_Click(Object param)
        {
            var paramss = param as Craft;
            Crafts = paramss;
            CraftEdit = new Pages.Setting.Craft_Edit3();
            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }
        private void SaveColumn_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            myDbContext.UpdateCraft(Crafts);
            GlobalVariables.CraftViewList3 = MyDbContext.GetCraftViewByType(4);
            MessageBox.Show("修改成功！");
            CraftEdit.Close();//关闭弹窗
        }
        private void EditProject_Click(Object param)
        {
            var paramss = param as Craft;
            Crafts = paramss;
            Pages.Setting.Craft_Edit3 CraftEdit = new();
            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }
     
        // Swap 方法用于交换数组中两个位置的对象
        void Swap(List<Craft> array, int index1, int index2)
        {
            if (array == null)
            {
                return;
            }
            int tmpId = array[index1].Id;
            array[index1].Id = array[index2].Id;
            array[index2].Id = tmpId;
        }

      
        private void SaveProject_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            myDbContext.UpdateCraft(Crafts);
        }
        private async void AddProject_Click()
        {
            var craft = new Craft
            {
                Project = "变压器温度表名称",
                Speed = 50,
                StartPoint = GlobalVariables.GreatestPressure,
                EndPoint = 0,
                Type = crafttype,
                ShowType = "变压器温度表"
            };
            CraftList.Add(craft);

            using MyDbContext myDbContext = new();
            await myDbContext.AddAsync<DataBase.DBmodels.Craft>(craft);

        }
        private async void DelProject_Click(object param)//删除项目
        {
            var paramss = param as Craft;
            using MyDbContext myDbContext = new();
            await myDbContext.DeleteAsync<Craft>(paramss);
            CraftList.Remove(paramss);
        }
        #endregion
    }
}
